import { ComSeriveProvider } from './../../../providers/com-serive/com-serive';
import { LinergradientServiceProvider } from './../../../providers/linergradient-service/linergradient-service';
import { Component } from '@angular/core';
import {
  IonicPage,
  NavController,
  NavParams,
  ToastController
} from 'ionic-angular';
import { BaseUI } from '../../../common/baseUI';

@IonicPage()
@Component({
  selector: 'page-forget-password',
  templateUrl: 'forget-password.html'
})
export class ForgetPasswordPage extends BaseUI {
  isShow: Boolean = true; //判断是否显示倒计时
  count: number = 60; // 倒计时时间
  toastCount = 0; // 土司间隔时间
  timer: any = null; // 定时器的名称
  phone = ''; // 双向绑定手机号码
  code = ''; // 验证码 
  password: string = '';
  repassword: string = '';
  AllZeroCode: string;
  isRegister = true;

  phones = '';
  codes = '';

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public toastCtrl: ToastController,
    public lgService: LinergradientServiceProvider,
    public comSerive: ComSeriveProvider
  ) {
    super();
  }

  ionViewDidLoad() {}

  gotoUpDataInfo() {
    const reg = /^1[34578][0-9]{9}$/;

    if (reg.test(this.phone)) {
      // 如果验证码前缀是0 会自动省略
      if ((this.code + '').length === 5) {
        this.AllZeroCode = '0' + this.code;
      } else if ((this.code + '').length === 6) {
        this.AllZeroCode = this.code.toString();
      } else if (
        (this.code + '').length === undefined ||
        (this.code + '').length === null
      ) {
        this.isShowToast('验证码不能为空');
      }
    }

    const stringverification = JSON.stringify({
      verification: this.AllZeroCode,
      phoneNumber: this.phone
    });
    const user = { phone: this.phone, password: this.repassword };
    if (this.phone === undefined) {
      this.isShowToast('手机号不能为空');
    } else if (!reg.test(this.phone)) {
      this.isShowToast('手机格式不正确');
    } else if (this.code === null) {
      this.isShowToast('请检查验证码');
    } else if (this.password.length < 8) {
      this.isShowToast('密码长度需要大于8位');
    } else if (this.password.length > 16) {
      this.isShowToast('密码长度需要小于16位');
    } else if (this.password !== this.repassword) {
      this.isShowToast('密码与确认密码不符');
    } else if (
      this.password.length >= 8 &&
      this.password.length <= 16 &&
      this.repassword === this.password
    ) {
      this.comSerive.checkCode(stringverification).subscribe(data => {
        // console.log(data);
        if (data['status'] === 200) {
          this.isShowToast('修改成功');
          this.comSerive.changPassword(user).subscribe(data => {
            this.navCtrl.pop();
          });
        } else {
          this.isShowToast('验证码错误');
        }
      });
    }
  }

  setCode() {
    const reg = /^1[34578][0-9]{9}$/;
    if (reg.test(this.phone)) {
      this.comSerive.changePasswordCode(this.phone).subscribe(data => {
        if (data['status'] === 200) {
          this.isShow = false;
          this.setTiem();
          // console.log(data);
        } else {
          this.isShowToast('发送次数已达到上限');
          // console.log(data);
        }
      });
    } else {
      this.isShowToast('手机格式不正确');
    }
  }

  // 设计定时器
  setTiem() {
    clearInterval(this.timer);
    this.timer = setInterval(() => {
      this.count--;
      if (this.count === 0) {
        this.count = 60;
        this.isShow = true;
        clearInterval(this.timer);
      }
    }, 1000);
  }

  keyup() {
    const reg = /^1[34578][0-9]{9}$/;
    if (reg.test(this.phone)) {
      this.comSerive.checkPhoneInfo(this.phone).subscribe(data => {
        if (data['status'] === 200) {
          this.isShowToast('账号未注册');
          this.isRegister = true;
        } else {
          this.isRegister = false;
        }
      });
    }
  }

  rep($event) {
    //删除键
    if ($event.keyCode == 8 && this.phone.length == 0) {
      this.phone = this.phones = '';
      return;
    }
    //通过phone保存值的改变 来判断用户是否输入非法字符
    //输入非法字符后input的值会突然为空
    if (this.phone != '') {
      this.phones = this.phone;
    } else {
      this.phone = this.phones == '' ? '中文触发number' : this.phones;
    }
    this.phone = this.phone.replace(/[eE]/, '');
    // console.log(this.phone);
    if (this.phone.length > 11) this.phone = this.phone.slice(0, 11);
    this.keyup();
  }

  codeNum($event) {
    //删除键
    if($event.keyCode == 8 && this.code.length == 0){
      this.code = this.codes = "";
      return; 
    }
    //通过phone保存值的改变 来判断用户是否输入非法字符
    //输入非法字符后input的值会突然为空
    if(this.code != ""){
      this.codes = this.code;
    }else{
      this.code = this.codes == "" ? "中文触发number" : this.codes;
    }
    this.code = this.code.replace(/[eE\.]/, '');
    // console.log(this.phone);
    if(this.code.length>6) this.code=this.code.slice(0,6)
}

  // 限制重复弹窗
  isShowToast(message) {
    if (this.toastCount === 0) {
      this.showToast(this.toastCtrl, message);
      this.toastCount++;
      setTimeout(() => {
        this.toastCount = 0;
      }, 3000);
    }
  }
  pop() {
    this.navCtrl.pop();
  }
}
